from code.data.Notice import Notice
from code.data.User import User
from code.dao.notice_management import search_notice,delete_notice,add_notice,update_notice
from code.dao.user_management import search_user


class NoticeBackend:
    @staticmethod
    def notice_display():
        ret = 0
        notice_list = search_notice(Notice(), "all")
        if notice_list is None:
            ret = 3001
        else:
            notice_list.sort(key=lambda notice: notice.priority,reverse = True)
            ret = notice_list
        return ret

    @staticmethod
    def notice_delete(userin:User, noticein:Notice):
        ret = 0
        if userin.userID is None:
            ret = 3010
        elif noticein.noticeID is None:
            ret = 3011
        else:
            user = search_user(userin, "userID")[0]
            notice = search_notice(noticein, "noticeID")[0]
            if user.userID == notice.userID:
                if delete_notice(notice) is True:
                    ret = 1
                else:
                    ret = 0
            else:
                ret = 3002
        return ret

    @staticmethod
    def notice_to_top(userin: User, noticein: Notice):
        ret = 0
        if userin.userID is None:
            ret = 3010
        elif noticein.noticeID is None:
            ret = 3011
        else:
            user = search_user(userin, "userID")[0]
            notice = search_notice(noticein, "noticeID")[0]
            if user.userID != notice.userID:
                ret = 3004
            if notice.priority != 0 or notice.priority  is not None:
                ret = 3003
            else:
                notice.priority = 1
                if update_notice(notice, "priority") is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def notice_not_to_top(userin:User, noticein:Notice):
        ret = 0
        if userin.userID is None:
            ret = 3010
        elif noticein.noticeID is None:
            ret = 3011
        else:
            user = search_user(userin, "userID")[0]
            notice = search_notice(noticein, "noticeID")[0]
            if user.userID == notice.userID:
                if notice.priority == 1:
                    notice.priority = 0
                    if update_notice(notice, "priority") is True:
                        ret = 1
                    else:
                        ret = 0
                else:
                    ret = 3008
            else:
                ret = 3004
        return ret

    @staticmethod
    def notice_announce(userin: User, notice:Notice):
        ret = 0
        if userin.userID is None:
            ret = 3010
        else:
            user = search_user(userin, "userID")[0]
            if user.type == "Teacher" or user.type == "admin":
                if notice.content is None:
                    ret = 3005
                elif len(notice.content) == 0 or notice.content.isspace():
                    ret = 3005
                elif notice.type != "作业" and notice.type != "考试" and notice.type != "通知":
                    ret = 3007
                elif len(notice.content) > 255:
                    ret = 3009
                elif user.userID != notice.userID:
                    ret = 3011
                else:
                    notice.userID = user.userID
                    notice.priority = 0
                    if add_notice(notice) is True:
                        ret = 1
                    else:
                        ret = 0
            else:
                ret = 3006
        return ret


if __name__ == '__main__':
  #  print(" ", len(" "))
  print(" user:",search_user(User(userID="admin"),"userID"))
   # noti = Notice(content = "打发打发")
   # print("3005:", NoticeBackend.notice_announce(User(type= "teacher"), noti))

  #  print("3005:", NoticeBackend.notice_announce(User(type= "teacher"), Notice(content = "  ")))

  #  print("3007:", NoticeBackend.notice_announce(User(type= "teacher", userID="2019300003077"), Notice(content = "sdsd", userID="2019300003001", priority=1, type="作业")))

   # print("3007:", NoticeBackend.notice_announce(User(type="teacher", userID="2019300003077"),Notice(content="的发动机比利时的", userID="2019300003001", priority=0,type="作业")))

    #print("3007:", NoticeBackend.notice_announce(User(type="teacher", userID="2019300003077"), Notice(content="的发动机比利时的得分得分", userID="2019300003001", priority=1, type="作业")))


   # print(" 1:",NoticeBackend.notice_to_top(User(userID="2019300003077"), Notice(userID = "2019300003077",noticeID="2")))
    #print(" 3003:", NoticeBackend.notice_to_top(User(userID="2019300003077"), Notice(userID="2019300003077",noticeID="2")))

   # print(" 1:",NoticeBackend.notice_to_top(User(userID="2019300003077"), Notice(userID = "2019300003077",noticeID="3")))

    #print("公告列表:1",NoticeBackend.notice_display()[0].content,NoticeBackend.notice_display()[0].noticeID,"  ",NoticeBackend.notice_display()[1].content," ",NoticeBackend.notice_display()[2].content," ", NoticeBackend.notice_display()[3].content ," " ,NoticeBackend.notice_display()[4].content)